<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $technicianIdDetailArray = array();
  $technicianName          = "";
  $firstName               = "";
  $lastName                = "";
  $address                 = "";
  $city                    = "";
  $mobile1                 = "";
  $mobile2                 = "";
  $phone                   = "";
  $email                   = "";
  $image                   = "";
  $i                       = 0;  
  $j                       = "";  
  $technicianId            = isset($_REQUEST['technicianId']) ? $_REQUEST['technicianId'] : 0;

  // after post : start 
  if(isset($_POST['firstName']))
  {
  	// upload Image Code
    	if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/pjpeg"))
            && ($_FILES["file"]["size"] < 2000000000))
      {
        if ($_FILES["file"]["error"] > 0)
        {
          echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
          echo "Upload: " . $_FILES["file"]["name"] . "<br />";
          echo "Type: " . $_FILES["file"]["type"] . "<br />";
          echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
          echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

          if (file_exists("upload/" . $_FILES["file"]["name"]))
          {
            echo $_FILES["file"]["name"] . " already exists. ";
          }
          else
          {
          	$filename  =  pathinfo($_FILES["file"]["name"]);
          	$extension = $filename['extension']; 
          	$j = "img_".rand(100,999).".".$extension;
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/".$j);
            echo "Stored in: " . "upload/".$j;
          }
        }
      }
    else
    {
      echo "Invalid file";
    }
    // upload Image Code Over
    
    if($technicianId > 0)
    {
      if($_FILES["file"]["name"] == "")
        $file = $_POST["imgFile"];
      else
        $file = $j;
      
      $updateTechnician = "UPDATE technician SET firstName    = '".$_POST['firstName']."',
                                                 lastName     = '".$_POST['lastName']."',
                                                 address      = '".$_POST['address']."',
                                                 city         = '".$_POST['city']."',   
                                                 mobile1      = '".$_POST['mobile1']."',   
                                                 mobile2      = '".$_POST['mobile2']."',   
                                                 phone        = '".$_POST['phone']."',   
                                                 email        = '".$_POST['email']."',   
                                                 image        = '".$file."'   
                                           WHERE technicianId = '".$_REQUEST['technicianId']."'";
      $updateTechnicianRes  = mysql_Query($updateTechnician);
      if(!$updateTechnicianRes)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:technician.php");
    }
    else
    {
      $userId = $_SESSION['s_activeUserId'.$cfgPack];
      $address = addslashes($_POST['address']);
      $insertTechnician = "INSERT INTO technician (userId,firstName,lastName,address,city,mobile1,
                                                   mobile2,phone,email,image)
                            VALUE('".$userId ."','".$_POST['firstName']."','".$_POST['lastName']."',
                                  '".$address."','".$_POST['city']."','".$_POST['mobile1']."',
                                  '".$_POST['mobile2']."','".$_POST['phone']."','".$_POST['email']."',
                                  '".$j."')";
      $insertTechnicianResult = mysql_query($insertTechnician);
      if(!$insertTechnicianResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:technician.php");
    }
  }    
  // after post : end
  
  $selectTechnician = "SELECT technicianId,firstName,lastName,address,city,mobile1,mobile2,phone,email,image
                         FROM technician
                     ORDER BY firstName";
  $selectTechnicianResult = mysql_query($selectTechnician);
  $itemInRow = mysql_num_rows($selectTechnicianResult);
  while($itemInRow = mysql_fetch_array($selectTechnicianResult))
  {                                                    
    $technicianIdDetailArray[$i]['technicianId']    = $itemInRow['technicianId'];
    $technicianIdDetailArray[$i]['firstName']       = $itemInRow['firstName'];
    $technicianIdDetailArray[$i]['lastName']        = $itemInRow['lastName'];
    $technicianIdDetailArray[$i]['technicianName']  = $itemInRow['firstName']." ".$itemInRow['lastName'];
    $technicianIdDetailArray[$i]['address']         = $itemInRow['address'];
    $technicianIdDetailArray[$i]['city']            = $itemInRow['city'];
    $technicianIdDetailArray[$i]['mobile1']         = $itemInRow['mobile1'];
    $technicianIdDetailArray[$i]['mobile2']         = $itemInRow['mobile2'];
    $technicianIdDetailArray[$i]['phone']           = $itemInRow['phone'];
    $technicianIdDetailArray[$i]['email']           = $itemInRow['email'];
    $technicianIdDetailArray[$i]['image']           = $itemInRow['image'];
    echo "<br>". $technicianId;
    if($itemInRow['technicianId'] == $technicianId)
    {
    	$firstName       = $itemInRow['firstName']; 
    	$lastName        = $itemInRow['lastName']; 
      $address         = $itemInRow['address'];  
      $city            = $itemInRow['city'];  
      $mobile1         = $itemInRow['mobile1'];  
      $mobile2         = $itemInRow['mobile2'];  
      $phone           = $itemInRow['phone'];  
      $email           = $itemInRow['email'];  
      $image           = $itemInRow['image'];  
    }
    $i++;     
  }    
   
  include("./bottom.php");
  $smarty->assign("technicianId",$technicianId);
  $smarty->assign("firstName",$firstName);
  $smarty->assign("lastName",$lastName);
  $smarty->assign("technicianName",$technicianName);
  $smarty->assign("address",$address);
  $smarty->assign("city",$city);
  $smarty->assign("mobile1",$mobile1);
  $smarty->assign("mobile2",$mobile2);
  $smarty->assign("phone",$phone);
  $smarty->assign("email",$email);
  $smarty->assign("image",$image);
  $smarty->assign("technicianIdDetailArray",$technicianIdDetailArray);
  $smarty->display("technician.tpl");
}
?>